package cn.flyingroc.travelbook;

import java.util.ArrayList;

import cn.flyingroc.travelbook.model.POI;

import android.app.Activity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.widget.ArrayAdapter;
import android.widget.TextView;

public class UpdateAdapter extends ArrayAdapter {
	
    private static final String TAG = "UpdateAdapter";
    Activity context;
    LayoutInflater inflater;
    WebView userIcon;
    TextView updateBody, comment, timestamp;
    ArrayList<POI> updates;
    
    @SuppressWarnings("unchecked")
	public UpdateAdapter(Activity context, ArrayList<POI> Updates) {  
        super(context, R.layout.update_row, Updates);
        inflater = LayoutInflater.from(context);
        this.context = context;
        this.updates = Updates;
    }
    
    public View getView(int position, View convertView, ViewGroup parent) {  
        View row = convertView;
        UpdateWrapper wrapper;
        
        row = inflater.inflate(R.layout.update_row, null);
        wrapper = new UpdateWrapper(row);
        row.setTag(wrapper);
        row.setLongClickable(true);
        
        POI u = updates.get(position);
        
        
        userIcon = wrapper.getUserIcon();
        loadHTML(userIcon,u);
        
        
        updateBody = wrapper.getUpdateBody();
        updateBody.setText(u.title);
        comment = wrapper.getComment();
        String desc = u.desc.length()>10?u.desc.substring(0,10)+"...":u.desc;
        comment.setText("描述: " + desc);
        timestamp = wrapper.getTimestamp();
        timestamp.setText(u.createdTime);
        
        return row;
    }

    public void loadHTML(WebView userIcon,POI u) {
    	userIcon.setPadding(0, 0, 0, 0);
    	final String path = Main.PHOTO_PATH;
        final String mimeType = "text/html";
        final String encoding = "utf-8";
        final String photoUrl = "file://"+path+u.photoName;
        userIcon.loadDataWithBaseURL("fake://not/needed", "<html><body><IMG width='40dip' height='50dip' src=\""+photoUrl+"\"/></body></html>",mimeType, encoding, null);
        //userIcon.loadUrl("<html><body><IMG width='40dip' height='50dip' src=\""+photoUrl+"\"/></body></html>");
    }
    
    @Override
    public boolean areAllItemsEnabled() {
        return true;
    }

    @Override
    public boolean isEnabled(int position) {
        return true;
    }
    

}
